package com.mayspeed.jg.util;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.mayspeed.jg.JingGong;

/**
 * 精工插件所需Util类
 * @author May_Speed
 * @since 2017/4/22
 */
public class JGUtil {
	
	/**
	 * 用精工等级得到对应的String
	 * @param is
	 * @return
	 */
	public static String getJingGongLevelToStarString(int level) {
		switch (level) {
		case 1:
			return "§e★§r☆☆☆☆";
		case 2:
			return "§e★★§r☆☆☆";
		case 3:
			return "§e★★★§r☆☆";
		case 4:
			return "§e★★★★§r☆";
		case 5:
			return "§e★★★★★";
		default:
			return "§e★§r☆☆☆☆";
		}
	}
	
	/**
	 * 取武器的精工等级
	 * @param is
	 * @return
	 */
	public static int getItemJingGongLevel(ItemStack is) {
		if (is != null && is.getType() != Material.AIR && is.hasItemMeta() && is.getItemMeta().hasLore()) {
			List<String> lore = new ArrayList<String>();
			lore.addAll(is.getItemMeta().getLore());
			for (String string : lore) {
				if (string.indexOf("§e★§r☆☆☆☆") != -1) {
					return 1;
				}else if (string.indexOf("§e★★§r☆☆☆") != -1) {
					return 2;
				}else if (string.indexOf("§e★★★§r☆☆") != -1) {
					return 3;
				}else if (string.indexOf("§e★★★★§r☆") != -1) {
					return 4;
				}else if (string.indexOf("§e★★★★★") != -1) {
					return 5;
				}
			}
		}
		return 0;
	}
	
	/**
	 * 取玩家的金钱
	 * @param player
	 * @return
	 */
	public static double getPlayerMoney(Player player) {
		return JingGong.getInstance().getEco().getBalance(Bukkit.getOfflinePlayer(player.getUniqueId()));
	}
	
	/**
	 * 增加玩家的金钱
	 * @param player 玩家
	 * @param money 金钱
	 */
	public static void addPlayerMoney(Player player, double money) {
		JingGong.getInstance().getEco().depositPlayer(Bukkit.getOfflinePlayer(player.getUniqueId()), money);
	}
	
	/**
	 * 拿走玩家的金钱
	 * @param player 玩家
	 * @param money 金钱
	 */
	public static void takePlayerMoney(Player player, double money) {
		JingGong.getInstance().getEco().withdrawPlayer(Bukkit.getOfflinePlayer(player.getUniqueId()), money);
	}
	
//	public static String getServerVersion() {
//		return Bukkit.getServer().getClass().getPackage().getName().substring(23);
//	}
	
	/**
	 * 反射: 取玩家手中物品
	 * @param player
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static ItemStack getItemInMainHand(Player player) {
		//判断是否为1.9+
		if (Bukkit.getBukkitVersion().split("-")[0].startsWith("1.9")) {
			try {
				//获取obc下的CraftInventoryPlayer
				Class<?> craftinventory = Class.forName("org.bukkit.craftbukkit." + JGUtil.getServerVersion() + ".inventory.CraftInventoryPlayer");
				//调用其方法getItemInMainHand
				return (ItemStack) craftinventory.getMethod("getItemInMainHand").invoke(player.getInventory());
			} catch (Exception e) {
				System.out.println("错误: " + e.getMessage());
				e.printStackTrace();
			}
		}
		return player.getItemInHand();
	}
	
	/**
	 * 取服务器版本
	 * @return
	 */
	public static String getServerVersion() {
		return org.bukkit.Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
	}
	
	/**
	 * 替换Lore
	 * @param is
	 * @param old
	 * @param newString
	 * @return
	 */
	public static ItemStack replaceLore(ItemStack is, String old, String newString) {
		ItemMeta im = is.getItemMeta();
	    List<String> lore = im.getLore();
	    if (!lore.contains(old)) {
	    	return is;
	    }
	    while (true) {
	    	if (!lore.contains(old)) {
	    		break;
	        }
	    	lore.set(lore.indexOf(old), newString);
	    }
	    im.setLore(lore);
	    is.setItemMeta(im);
	    return is;
	}
}
